// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const {
	pagination
} = require('pagination')
const unipay = require('uni-pay')
const collection = uniCloud.database().collection('order_list')
const unipayInit = unipay.initWeixin({
	"appId": "wx52d48be4d68f58a2", // 小程序的appid
	// "secret": "e1e5894c7b5a4e005f84737be6b8ee31", // 小程序的secret
	"mchId": "1628245715", // 商户id
	"key": "412728199807303111GYG07303111111",
})
module.exports = {
	_before: function() { // 通用预处理器

	},
	async getData(event) {
		let params = {
			dbName: 'order_list',
			...event
		}
		let data = await pagination(params)
		return {
			code: 200,
			msg: '获取订单列表成功',
			...data
		}
	},
	async searchData(event) {
		let result = await unipayInit.orderQuery({
			outTradeNo: event.outTradeNo
		})
		return {
			code: 200,
			msg: '获取订单详情成功',
			result
		}
	},
	async saveData(event) {
		let params = {
			...event
		}
		let result
		if (!event._id) {
			result = await collection.add(params)
		} else {
			delete params._id
			result = await collection.doc(event._id).update({
				...params
			})
		}
		return {
			code: 200,
			msg: '保存成功',
			...result
		}
	},
}